<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div v-if="showSearch" class="filter-container">
      <el-form :inline="true" :model="listQuery" class="form-inline">
        <el-form-item label="">
          <el-select
            v-model="listQuery.visitor"
            placeholder="访客类型"
            default-first-option
            clearable
            size="small"
          >
            <el-option label="全部" value="-1" />
            <el-option label="Human" value="human" />
            <el-option label="Google" value="google" />
            <el-option label="Bing" value="bing" />
            <el-option label="Baidu" value="baidu" />
            <el-option label="Sogou" value="sogou" />
            <el-option label="Yisou" value="yisou" />
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="listQuery.ip" placeholder="访客IP" clearable size="small" />
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="listQuery.uri" placeholder="访问地址" clearable size="small" />
        </el-form-item>
        <el-form-item label="">
          <el-select v-model="listQuery.visitTime" placeholder="访问时间" default-first-option size="small">
            <el-option label="全部" value="all" />
            <el-option label="昨天" value="yesterday" />
            <el-option label="今天" value="today" />
            <el-option label="本周" value="week" />
            <el-option label="本月" value="month" />
            <el-option label="本季度" value="quarter" />
            <el-option label="本年" value="year" />
            <el-option label="自定义" value="custom" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="listQuery.visitTime === 'custom'" label="">
          <el-date-picker
            v-model="dateTime"
            :picker-options="pickerOptions"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            align="right"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="primary" icon="el-icon-search" size="small" @click="handleFilter">
            搜索
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="warning" icon="el-icon-refresh" size="small" @click="handleFilterClear">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作 -->
    <el-row style="margin-bottom: 10px;">
      <el-col :xs="24" :sm="24" :lg="24">
        <el-tooltip content="返回统计列表" placement="top">
          <el-button v-waves type="success" icon="el-icon-refresh-left" circle @click="handleBack" />
        </el-tooltip>
        <el-tooltip content="刷新" placement="top">
          <el-button v-waves type="warning" icon="el-icon-refresh" circle @click="handleFilterClear" />
        </el-tooltip>
        <el-tooltip content="搜索" placement="top">
          <el-button v-waves type="primary" icon="el-icon-search" circle @click="showSearch = !showSearch" />
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button
            v-waves
            :loading="deleting"
            :disabled="buttonDisabled"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handleDeleteAll"
          />
        </el-tooltip>
        <el-tooltip content="清空" placement="top">
          <el-button v-waves type="danger" icon="el-icon-delete-solid" circle @click="handClear" />
        </el-tooltip>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" align="center" width="100" prop="id" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="visitor" width="100px" prop="visitor" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.visitor }}</span>
        </template>
      </el-table-column>
      <el-table-column label="IP" min-width="100px" prop="ip" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="访问时间" width="160px" align="center" prop="visitTime" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.visitTime|parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="访问地址" width="150px" prop="uri" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.uri }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作系统" width="150px" align="center" prop="os" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.os }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览器" width="150px" align="center" prop="browser" sortable="custom">
        <template slot-scope="scope">
          <span>{{ scope.row.browser }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination v-show="total>0" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.psize" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import { getList, delAll, clear } from '@/api/visit'
import waves from '@/directive/waves'
import { pickerOptions, getArrByKey } from '@/utils'

export default {
  name: 'Visit',
  components: { },
  directives: {
    waves
  },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      showSearch: false,
      listQuery: {
        page: 1,
        psize: 10,
        sort: 'id desc',
        visitor: '-1',
        ip: '',
        uri: '',
        visitTime: '',
        startTime: '',
        endTime: ''
      },
      buttonDisabled: true,
      deleting: false,
      dateTime: '',
      pickerOptions: pickerOptions
    }
  },
  watch: {
    dateTime: function(val) {
      this.listQuery.startTime = val[0]
      this.listQuery.endTime = val[1]
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchList()
    },
    handleFilterClear() {
      this.listQuery = {
        page: 1,
        psize: 10,
        sort: 'id desc',
        visitor: '-1',
        ip: '',
        uri: '',
        visitTime: '',
        startTime: '',
        endTime: ''
      }
      this.dateTime = ''
      this.fetchList()
    },
    handleSizeChange(val) {
      this.listQuery.psize = val
      this.fetchList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchList()
    },
    // 排序
    sortChange(data) {
      const { prop, order } = data
      if (prop !== null) {
        this.listQuery.sort = prop + ' ' + (order === 'ascending' ? 'asc' : 'desc')
        this.fetchList()
      }
    },
    // 返回统计列表
    handleBack() {
      this.$router.push({
        path: '/log/visits'
      })
    },
    // 多选
    handleSelectionChange(val) {
      this.buttonDisabled = !(val.length > 0)
      this.selectedRows = val
    },
    // 永久删除
    handleDeleteAll() {
      const msg = '此操作将所选访问日志永久删除, 是否继续?'
      if (this.selectedRows.length > 0) {
        this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleting = true
          const ids = getArrByKey(this.selectedRows, 'id')
          const idstr = ids.join(',')
          delAll({ ids: idstr }).then(response => {
            if (response.status === 1) {
              this.fetchList()
              this.$message.success(response.msg)
            } else {
              this.$message.error(response.msg)
            }
            this.deleting = false
          }).catch((error) => {
            this.deleting = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.$message.error('请选择要删除的数据')
      }
    },
    // 清空
    handClear() {
      this.$confirm('谨慎!此操作将清空访问日志', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleting = true
        clear().then(response => {
          if (response.status === 1) {
            this.fetchList()
            this.$message.success(response.msg)
          } else {
            this.$message.error(response.msg)
          }
          this.deleting = false
        }).catch((error) => {
          this.deleting = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消清空'
        })
      })
    }
  }
}
</script>
